package projectx.core.base;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.log4j.Logger;

public final class ProjectXContext {
	private static final Logger _log = Logger.getLogger(ProjectXContext.class
			.getName());

	private static boolean _initialized;
	private static Context _context;

	public static void initialize() throws ProjectXSystemException {

		if (!_initialized) {

			synchronized (ProjectXContext.class) {

				if (!_initialized) {
					init();
				}
			}
		}
	}

	private static void init() throws ProjectXUnexpectedException {

		try {

			_context = new InitialContext();

			_log.info("Initialized successfully");

			_initialized = true;
		} catch (NamingException e) {
			throw new ProjectXUnexpectedException(e);
		}
	}

	public static Object lookup(String name) throws ProjectXUnexpectedException {

		try {

			return _context.lookup(name);
		} catch (NamingException e) {
			throw new ProjectXUnexpectedException(e);
		}
	}

	public static boolean isInitialized() {
		return _initialized;
	}

	private ProjectXContext() {
	}
}
